<%@ page contentType="text/html; charset=UTF-8"%>
<%@ page import="java.util.*"%>
<%@ include file="../../common.jsp"%>
<html>
<head>
<title>库存领用录入</title>
<META http-equiv=Content-Type content="text/html; charset=UTF-8">
<LINK href="<%=root%>/css/caigouxitong.css" type=text/css rel=stylesheet>
<SCRIPT language="JavaScript" src="<%=root%>/js/common.js"></SCRIPT>
<SCRIPT language="JavaScript" src="<%=root%>/js/calendar.js"></SCRIPT>
<SCRIPT language="JavaScript" src="<%=root%>/js/string.js"></SCRIPT>
</head>

<%
    ArrayList tOrderDeptList = (ArrayList) request.getAttribute("orgware");
    ArrayList tShipMentList = (ArrayList) request.getAttribute("orgship");
    GSOperatorBean oper = (GSOperatorBean) session.getAttribute(GSBaseConstants.LOGIN);
    String shipid = request.getParameter("shipid");
    if (shipid == null) {
        shipid = oper.getGs_oper_org_id();
    }
    String StocksPriceUpdate=GSSystemConfigurator.getParam("gs.StocksPriceUpdate");
    String tStr="text";
    String style = "style='display:none;'";
    if(StocksPriceUpdate.equalsIgnoreCase("false")){
        tStr="hidden";
        style="";
    }
%>
<body BGCOLOR="#f2f2f2" text="#000000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<center>

<table width="98%" border="0" cellspacing="0" cellpadding="0" align="center">
  <tr>
    <td height="20">
    <table width="100%" border="0" cellspacing="0" cellpadding="1">
      <tr height="20">
        <td height="20" width="2" class="mtxt_black">&nbsp;</td>
        <td height="30" width="758" class="mtxt_black">库存领用录入</td>
      </tr>
    </table>
    </td>
  </tr>
  <tr>
    <td class="bg_line" height="2"></td>
  </tr>
</table>
<form name="form1" id="form1" method="post">
<table width="98%" border="0" cellspacing="0" cellpadding="0" align="center">
  <tr>
    <td>
    <table class=bg_table id=tableId cellspacing=1 cellpadding=2 width=100% align=center border=0>
      <tbody>
        <tr class="bg_column" height=20>
          <td colspan="4">领用单整体信息</td>
        </tr>
        <tr class="bg_row_odd" height=20>
          <td class=top align=middle>
          <div align="center" class="txt_content">领用单编号</div>
          </td>
          <td class=top><input type='text' id='stocksid' name='stocksid' size="20" maxlength="20" value="<%=request.getParameter("stocksid")==null?"":request.getParameter("stocksid") %>" onblur="selectId(this);">&nbsp; <font color="red">*</font> <font color="red" id='fn'></font></td>
          <td class=top align=middle>
          <div align="center" class="txt_content">领用单位</div>
          </td>
          <td class=top><select name="orgid" id="orgid">
            <option value="0000">---请选择---</option>
            <%
                String tOrderDept = request.getParameter("orgid");
                if (tOrderDeptList != null && tOrderDeptList.size() > 0) {
                    if (tOrderDept != null && tOrderDept.equals("0000")) {
                        for (int i = 0; i < tOrderDeptList.size(); i++) {
                            GSOrganizationBean temp = (GSOrganizationBean) tOrderDeptList.get(i);
            %>
            <option value="<%=temp.getGs_org_id()%>"><%=temp.getGs_org_name()%></option>
            <%
                }
                    }
                    else {
                        for (int i = 0; i < tOrderDeptList.size(); i++) {
                            GSOrganizationBean temp = (GSOrganizationBean) tOrderDeptList.get(i);
                            if (tOrderDept != null && tOrderDept.equals(temp.getGs_org_id())) {
            %>
            <option value="<%=temp.getGs_org_id()%>" selected><%=temp.getGs_org_name()%></option>
            <%
                }
                            else {
            %>
            <option value="<%=temp.getGs_org_id()%>"><%=temp.getGs_org_name()%></option>
            <%
                }
                        }
                    }
                }
            %>
          </select>&nbsp; <font color="red">*</font></td>
        </tr>
        <tr class="bg_row_odd" height=20>
          <td class=top align=middle>
          <div align="center" class="txt_content">领取人</div>
          </td>
          <td class=top><input type='text' id='stocksperson' name='stocksperson' size="20" value="<%=request.getParameter("stocksperson")==null?"":request.getParameter("stocksperson") %>">&nbsp; <font color="red">*</font></td>
          <td class=top align=middle>
          <div align="center" class="txt_content">领取部门</div>
          </td>
          <td class=top><input type='text' id='stocksdept' name='stocksdept' size="20" value="<%=request.getParameter("stocksdept")==null?"":request.getParameter("stocksdept") %>"></td>
        </tr>
        <tr class="bg_row_odd" height=20>
          <td class=top align=middle>
          <div align="center" class="txt_content">领取日期</div>
          </td>
          <td class=top><input type='text' id='stocksdate' name='stocksdate' size="20" onfocus="setday(this);" value=<%=DateUtil.getCurrentDate("yyyy-MM-dd") %>> <input
            type="hidden" name="stocksstate" value="0"></td>
          <td class=top align=middle>
          <div align="center" class="txt_content">出货单位</div>
          </td>
          <td class=top><select name="shipid" id="shipid">
            <%
                String shipdept = shipid;
                if (tShipMentList != null && tShipMentList.size() > 0) {
                    for (int i = 0; i < tShipMentList.size(); i++) {
                        GSOrganizationBean temp = (GSOrganizationBean) tShipMentList.get(i);
                        if (shipdept != null && shipdept.equals(temp.getGs_org_id())) {
            %>
            <option value="<%=temp.getGs_org_id()%>" selected><%=temp.getGs_org_name()%></option>
            <%
                }
                        else {
            %>
            <option value="<%=temp.getGs_org_id()%>"><%=temp.getGs_org_name()%></option>
            <%
                }
                    }
                }
            %>
          </select>&nbsp; <font color="red">*</font></td>
        </tr>
        <tr class="bg_row_odd" height=20>
          <td class=top align=middle>
          <div align="center" class="txt_content">备注</div>
          </td>
          <td class=top colspan="3">
          <textarea rows="3" cols="70" name="stocksremark" id="stocksremark"></textarea>&nbsp;<font color="red">长度不能超过200个字符</font></td>
        </tr>
      </tbody>
    </table>
    </td>
  </tr>
  <tr>
    <td>
    <table id=mx cellspacing=1 cellpadding=0 width=100% align=center border=1 bordercolor="black">
      <tr class="bg_column" height=20>
        <td colspan="10">领用明细<input type="hidden" id="currentrownum" value="0"></td>
      </tr>
      <tr class="bg_column" height=20>
        <td class=top align=middle><input type="checkbox" id="delall" onclick="alldell()"></td>
        <td class=top align=middle>
        <div align="center" class="txt_content">编号&nbsp; <font color="red">*</font></div>
        </td>
        <td class=top align=middle>
        <div align="center" class="txt_content">名称</div>
        </td>
        <td class=top align=middle>
        <div align="center" class="txt_content">供应商</div>
        </td>
        <td class=top align=middle>
        <div align="center" class="txt_content">单价(元)&nbsp; <font color="red">*</font></div>
        </td>
        <td class=top align=middle>
        <div align="center" class="txt_content">数量&nbsp; <font color="red">*</font></div>
        </td>
        <td class=top align=middle>
        <div align="center" class="txt_content">合计(元)</div>
        </td>
        <td class=top align=middle>
        <div align="center" class="txt_content">结算模式</div>
        </td>
        <td class=top align=middle>
        <div align="center" class="txt_content">用途</div>
        </td>
        <td class=top align=middle>
        <div align="center" class="txt_content">备注</div>
        </td>
      </tr>
    </table>
    <table id=mx2 cellspacing=1 cellpadding=0 width=100% align=center border=1 bordercolor="black">
      <tr class="bg_column" height=20>
        <td align="right" nowrap="nowrap">合计金额(元)：<input type='hidden' name='total' id='total' value="0.00"></td>
        <td>
        <div align="center" id='divtotal'>0.00</div>
        </td>
      </tr>

    </table>
    </td>
  </tr>
</table>

<br>
<input type="button" class="kuang" onclick="selectManufacture();" value="添加明细">&nbsp; <input type="button" class="kuang" onclick="deltablerow();" value="移除明细">&nbsp;
<input type="button" class="kuang" onclick="save();" value="保存"></form>
<script type="text/javascript">
    function selectManufacture(){
        var url="<%=response.encodeURL(root+"/A181104.do")%>";
        window.open(url,null," center=Yes, help=No, resizable=Yes, status=Yes, width=960px, height=680px, scrollbars=yes");     
    }
    function addtablerow(){
        var otr = document.getElementById("mx").insertRow(-1);
        otr.setAttribute("class","bg_row_odd");
        var td1=document.createElement("td");
        td1.setAttribute("class","top");
        td1.setAttribute("align","center");
        td1.innerHTML = "<input type='checkbox' name='checkItem' onclick='checkSelect();'><input type='hidden' name='sdetailstate' id='sdetailstate' value='0'><input type='hidden' name='sdetailproid' id='sdetailproid'><input type='hidden' name='sdetailconid' id='sdetailconid'><input type='hidden' name='sdetailcatid' id='sdetailcatid'><input type='hidden' name='sdetailmantype' id='sdetailmantype'>";
        var td2=document.createElement("td");
        td2.setAttribute("class","top");
        td2.setAttribute("align","center");
        td2.setAttribute("width","15%");
        td2.setAttribute("nowrap","nowrap");
        td2.innerHTML ="<div id='divsdetailmanid' name='divsdetailmanid'></div><input type='hidden' name='sdetailmanid' id='sdetailmanid'>";
        var td3=document.createElement("td");
        td3.setAttribute("class","top");
        td3.setAttribute("align","center");
        td3.setAttribute("width","15%");
        td3.innerHTML ="<div id='divsdetailmanname' name='divsdetailmanname'></div><input type='hidden' name='sdetailmanname' id='sdetailmanname'>";
        var td4=document.createElement("td");
        td4.setAttribute("class","top");
        td4.setAttribute("align","center");
        td4.setAttribute("width","9%");
        td4.innerHTML ="<a herf='#'  id='divproname' name='divproname'></a>";
        var td5=document.createElement("td");
        td5.setAttribute("class","top");
        td5.setAttribute("align","center");
        td5.innerHTML ="<input type='<%=tStr%>' name='sdetailunit' id='sdetailunit' size='9' onblur='formatordercount(this)'><div id='div1' name='div1' <%=style%>></div>";
        var td6=document.createElement("td");
        td6.setAttribute("class","top");
        td6.setAttribute("align","center");
        td6.innerHTML ="<input type='text' name='sdetailcount' id='sdetailcount' size='9' onblur='formatordercount(this)'>";
        var td7=document.createElement("td");
        td7.setAttribute("class","top");
        td7.setAttribute("align","center");
        td7.setAttribute("width","8%");
        td7.setAttribute("nowrap","nowrap");
        td7.innerHTML ="<div id='divsdetailsum' name='divsdetailsum'></div><input type='hidden' name='sdetailsum' id='sdetailsum'>";
        var td8=document.createElement("td");
        td8.setAttribute("class","top");
        td8.setAttribute("align","center");
        td8.setAttribute("width","8%");
        td8.setAttribute("nowrap","nowrap");
        td8.innerHTML ="<div id='divmode' name='divmode'></div><input type='hidden' name='mode' id='mode'>";
        var td9=document.createElement("td");
        td9.setAttribute("class","top");
        td9.setAttribute("align","center");
        td9.innerHTML ="<input type='text' name='sdetailuse' id='sdetailuse' size='15'>";
        var td10=document.createElement("td");
        td10.setAttribute("class","top");
        td10.setAttribute("align","center");
        td10.innerHTML ="<input type='text' name='sdetailremark' id='sdetailremark' size='15'>";
        otr.appendChild(td1);
        otr.appendChild(td2);
        otr.appendChild(td3);
        otr.appendChild(td4);
        otr.appendChild(td5);
        otr.appendChild(td6);
        otr.appendChild(td7);
        otr.appendChild(td8);
        otr.appendChild(td9);
        otr.appendChild(td10);        
    }
    function selectpro(a){
        var s="sdetailproid"+a;
        var b=document.getElementById(s).value;
        window.open("<%=response.encodeURL(root+"/A150504.do")%>?providerId="+b);
    }
  
    function deltablerow(){
        
        var c = document.getElementsByName('checkItem');
        var idArray = new Array();
        var a=0;
        for(var i=0; i<c.length; i++){
          if(c[i].checked){
            idArray.push(i);
            a++;
            }
        }
        if(a==0){
            alert("请选择要删除的行");
            return;
        }
        
        if(window.confirm("确定要移除选定行吗？")){
            var rowIndex;
            var nextDiff =0;
            for(j=0;j< idArray.length;j++) {
                rowIndex = idArray[j]+2-nextDiff++;                                                                                                             
                document.getElementById("mx").deleteRow(rowIndex);                                                                                                              
            }
        
            if(document.getElementsByName('checkItem').length ==0){
                document.getElementById('delall').checked=false;
            }
            sumtotal();
        }
    }
  
    function operation(index){
        var a="sdetailunit";
        var b="sdetailcount";
        var c="divsdetailsum";
        var d="sdetailsum";
        var unit=document.getElementsByName(a)[index].value;
        var count=document.getElementsByName(b)[index].value;
        var money=parseFloat(unit)*parseFloat(count);
        document.getElementsByName(c)[index].innerText=money.toFixed(4);
        document.getElementsByName(d)[index].value=money.toFixed(4);
        sumtotal();
    }
  
    function sumtotal(){
        var c = document.getElementsByName('checkItem');
        var num=0;
        for(var i=0;i<c.length;i++){
            var sd="sdetailsum";
            var ss=document.getElementsByName(sd)[i].value;
            num=parseFloat(num)+parseFloat(ss);
        }
        document.getElementById("divtotal").innerText=num.toFixed(4);
        document.getElementById("total").value=num.toFixed(4);
    }
  
    function checkSelect(){
        var c = document.getElementsByName('checkItem');
        
        for(var i=0; i<c.length; i++){
            if(!c[i].checked)
                break;
        }
        if(i<c.length){
            document.getElementById('delall').checked = false;
        } else {
            document.getElementById('delall').checked = true;
        }
    }
    
    function alldell() {
        var des =document.getElementsByName('checkItem');
        for(var i=0;i<des.length;i++) {
           if(des[i].checked=document.getElementById('delall').checked){
               des[i].parentNode.parentNode.className="checkBg";
               des[i].parentNode.nextSibling.firstChild.className="checkTxt";
               des[i].parentNode.nextSibling.nextSibling.firstChild.className="checkTxt";
           } else { 
               des[i].parentNode.parentNode.className="";
               des[i].parentNode.nextSibling.firstChild.className="";
               des[i].parentNode.nextSibling.nextSibling.firstChild.className="";}
        }
    }
    
    function formatordercount(obj){
        obj.value = FormatNum2(obj);
        getMoneyIndex(obj);
    }
    
    function getMoneyIndex(obj)
    {
        var name = obj.name;
        var items = document.getElementsByName(name);
        for(var i=0;i<items.length;i++) {
          if(items[i] == obj) {
            operation(i);
            break;
          }
        }
    }
    function save(){
        if(checkvalue()){
            form1.action="<%=response.encodeURL(root+"/A181106.do")%>";
            form1.submit();
        }
    }
    function checkvalue(){
        if(document.all.stocksid.value==null||Trim(document.all.stocksid.value)==""||Trim(document.all.stocksid.value.length)>20){
            alert("请正确输入领用单编号");
            return false;
        }
        if(Trim(document.getElementById('fn').innerText)!=""){
            alert(document.getElementById('fn').innerText);
            document.all.stocksid.focus();
            return false;
        }
        if(document.all.orgid.value=="0000"){
            alert("请选择领用单位");
            return false;
        }
        
        if(document.all.stocksperson.value==null||Trim(document.all.stocksperson.value)==""){
            alert("请输入领用人");
            return false;
        }
        if(Trim(document.all.stocksremark.value.length)>200){
            alert("备注长度不能超过200字符");
            return false;
        }
        var c = document.getElementsByName('checkItem');
        if(c.length<=0){
           alert("至少输入一项领用明细");
           return false;
        }
        for(var i=0;i<c.length;i++){
            var cv=c[i].value;
            var sd="sdetailunit";
            var sd2="sdetailcount";
            var ss=document.getElementsByName(sd)[i].value;
            var ss2=document.getElementsByName(sd2)[i].value;
            if(ss==""||isNaN(ss)||parseFloat(ss)<=0){
                alert("请正确输入单价");
                document.getElementsByName(sd)[i].focus();
                return false;
            }
            if(ss2==""||isNaN(ss2)||parseFloat(ss2)<=0){
                alert("请正确输入数量");
                          document.getElementsByName(sd2)[i].focus();
                          return false;
            }
        }
        return true;
    }
    var request = false;

    function selectId(a){

        if(Trim(a.value)==""){
            return false;
        }
        
        if(window.XMLHttpRequest) {  //非IE浏览器，用xmlhttprequest对象创建
            request=new XMLHttpRequest();
        } else if(window.ActiveXObject) { //IE浏览器用activexobject对象创建
            request=new ActiveXObject("Microsoft.XMLHttp");
        }
        
        if(request){
            var sid= a.value;
            var url = "<%=response.encodeURL(root+"/jsp/paymanage/stocksbyid.jsp")%>?sid=" + sid; 
            request.open("GET", url, true); 
            request.onreadystatechange = updatePage; 
            request.send(null);
        }else{
            document.getElementById("fn").innerText="";
        }
    }
    
    function updatePage(){
    var f=document.getElementById("fn");
        if(request.readyState == 4){      //已完成
            if(request.status == 200){ 
                var text = request.responseText;
                if(text.indexOf("true")==-1){//text为url页面的所有内容
                    f.innerText="编号已存在";
                }else{
                    f.innerText="";
                }
            }else 
                f.innerText="";
            } 
    }
</script>
</body>
</html>